package com.liyuncong.algorithm.algorithm_similarity.entity;

import java.util.BitSet;

import com.liyuncong.algorithm.algorithm_similarity.impl.EuclideanDistance;
import com.liyuncong.algorithm.algorithm_similarity.similarity.ComputeSimilarity;
import com.liyuncong.algorithm.algorithm_similarity.similarity.Distance;
import com.liyuncong.algorithm.algorithm_similarity.util.VectorUtil;


public class Vector implements Distance{
	// 存储向量元素
	private float[] vector;
	// 向量长度
	private float size;
	// 向量元素的个数
	private int length;
	// 记录该vector中非零元素的位置信息
	private BitSet bitSet;
	private ComputeSimilarity computeSimilarity = new EuclideanDistance();

	public Vector(float[] vector) {
		this.vector = vector;
		this.length = vector.length;
		this.size = VectorUtil.computeVectorSize(vector);
		this.bitSet = VectorUtil.getIndexsOfNonzero(vector);
	}

	public float[] getVector() {
		return vector;
	}

	public void setVector(float[] vector) {
		this.vector = vector;
	}

	public int getLength() {
		return length;
	}
	
	/**
	 * @return 返回向量长度
	 */
	public float getSize() {
		return size;
	}

	public BitSet getBitSet() {
		return bitSet;
	}

	/**
	 * @param index 向量中值的索引
	 * @return 向量中索引为index的值
	 */
	public float valueOf(int index) {
		return this.vector[index];
	}
	

	/**
	 * @param computeSimilarity 相似度计算的具体实现
	 */
	public void setComputeSimilarity(ComputeSimilarity computeSimilarity) {
		this.computeSimilarity = computeSimilarity;
	}

	public float distanceTo(Vector vectorOther) {
		return computeSimilarity.compute(this, vectorOther);
	}

}
